import numpy as np

# ---- run Test
num: int = 0


def runTest(fun):
    global num
    print('----------------------', num)
    fun()
    num += 1
    print()


# -------------------------------------------- tests:

def test_resize():
    a = np.array([[1, 2, 3], [4, 5, 6]])
    print(a)
    print('# a数组的形状')
    print(a.shape)
    b = np.resize(a, (3, 2))
    print('# b数组')
    print(b)
    print('# b数组的形状')
    print(b.shape)
    print('# 修改b数组使其形状大于原始数组')
    b = np.resize(a, (3, 3))
    print(b)


def test_append():
    a = np.array([[1, 2, 3], [4, 5, 6]])
    print('# 向数组a添加元素')
    print(np.append(a, [7, 8, 9]))
    print('# 沿轴 0 添加元素')
    print(np.append(a, [[7, 8, 9]], axis=0))
    print('# 沿轴 1 添加元素')
    print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))


def test_insert():
    a = np.array([[1, 2], [3, 4], [5, 6]])
    print('# 不提供axis的情况，会将数组展开')
    print(np.insert(a, 3, [11, 12]))
    print('# 沿轴 0 垂直方向')
    print(np.insert(a, 1, [11], axis=0))
    print('# 沿轴 1 水平方向')
    print(np.insert(a, 1, 11, axis=1))

    print(np.insert(a, 1, 11, axis=0))

    print('# https://numpy.org/devdocs/reference/generated/numpy.insert.html#numpy-insert')
    a = np.array([[1, 1], [2, 2], [3, 3]])
    print('# array a_')
    print(a)
    print('if axis=None, return flatten')
    c = np.insert(a, [2, 2], [5, 6])
    print(c)


def test_delete():
    a = np.arange(12).reshape(3, 4)
    print('# a数组')
    print(a)
    print('# 不提供axis参数情况')
    print(np.delete(a, 5))
    print('# 不改变原数组')
    print(a)

    print('# 删除第二列')
    print(np.delete(a, 1, axis=1))
    print('# 删除经切片后的数组')
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    print('np.s_[::2]', np.s_[::2])
    print(np.delete(a, np.s_[::2]))


def test_argwhere():
    x = np.arange(6).reshape(2, 3)
    print('原数组')
    print(x)
    print('# 返回所有大于1的元素索引')
    y = np.argwhere(x > 1)
    print(y)


def test_unique():
    a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])
    print('原数组')
    print(a)
    print('# 对a数组的去重')
    uq = np.unique(a)
    print(uq)
    print('# 打印去重后数组的索引')
    u, indices = np.unique(a, return_index=True)
    print(indices)
    print('# 去重数组的下标：')
    ui, indices = np.unique(a, return_inverse=True)
    print(indices)
    print('# 返回去重元素的重复数量')
    uc, indices = np.unique(a, return_counts=True)
    print('# 元素出现次数：')
    print(uc)
    print(indices)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    runTest(test_resize)
    runTest(test_append)
    runTest(test_insert)
    runTest(test_delete)
    runTest(test_argwhere)
    runTest(test_unique)
